package com.sanpu.kxe.business.main.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.adapter.MyProfitsAdapter;
import com.sanpu.kxe.business.main.bean.ProfitsOutput;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.contract.MyProfitsContract;
import com.sanpu.kxe.business.main.presenter.MyProfitsPresenter;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.view.ScrollableFragment;

import java.util.List;

import butterknife.Bind;

/**
 * Description：我的盟友列表（收益）
 * Created by 薛勇军 on 2018/8/7.
 */

public class MyProfitsFragment extends ScrollableFragment implements MyProfitsContract.View {

    @Bind(R.id.list_view)
    RecyclerView vListView;

    private int nowPage = 0;

    private MyProfitsAdapter mProfitsAdapter;

    private MyProfitsContract.Presenter mPresenter;

    public static MyProfitsFragment getInstance(int id, String merchantCode, String accountName) {
        MyProfitsFragment fragment = new MyProfitsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(MainConfig.INTENT_MERCHANTCODE, merchantCode);
        bundle.putString(MainConfig.INTENT_ACCOUNTNAME, accountName);
        bundle.putInt(MainConfig.INTENT_PROFITS_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_my_profits;
    }

    @Override
    protected void initView() {
        super.initView();
        int mCurLvl = (int) getArguments().get(MainConfig.INTENT_PROFITS_ID);
        String mMerchantCode = getArguments().getString(MainConfig.INTENT_MERCHANTCODE);
        String mAccountName = getArguments().getString(MainConfig.INTENT_ACCOUNTNAME);
        vListView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mProfitsAdapter = new MyProfitsAdapter(mCurLvl);
        vListView.setAdapter(mProfitsAdapter);
        mProfitsAdapter.disableLoadMoreIfNotFullPage(vListView);
        mProfitsAdapter.setPreLoadNumber(2);

        mPresenter = new MyProfitsPresenter(this, mCurLvl);
        mPresenter.initParamsData(mMerchantCode, mAccountName);
        if (!TextUtils.isEmpty(mMerchantCode) && !TextUtils.isEmpty(mAccountName)) {
            mPresenter.getMyProfitsTeam(nowPage);
        }
    }

    /**
     * 加载数据
     */
    public void loadData(String merchantCode, String accountName) {
        mPresenter.initParamsData(merchantCode, accountName);
        mPresenter.getMyProfitsTeam(nowPage);
    }

    @Override
    protected void setListener() {
        super.setListener();
        mProfitsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mPresenter.onItemClick(((MyProfitsAdapter)adapter).getItem(position));
            }
        });
        mProfitsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getMyProfitsTeam(nowPage);
            }
        }, vListView);
    }

    /**
     * 刷新数据
     */
    public void setOnRefresh() {
        nowPage = 0;
        mPresenter.getMyProfitsTeam(nowPage);
        mProfitsAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showProfitsTeamView(List<ProfitsOutput> profitsOutputs, boolean isFirst) {
        if (profitsOutputs != null && profitsOutputs.size()>0) {
            if (isFirst) {
                mProfitsAdapter.setNewData(profitsOutputs);
            } else {
                mProfitsAdapter.addData(profitsOutputs);
            }
            nowPage++;
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                mProfitsAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                mProfitsAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                mProfitsAdapter.loadMoreFail();
                break;
        }
    }

    /**
     * 获取当前merchantName
     */
    public String getMerchantName() {
        return mPresenter.getMerchantName();
    }

    /**
     * 获取当前级别总人数
     */
    public int getPersonCount() {
        return mPresenter.getPersonCount();
    }

    @Override
    public boolean canScrollVertically(int direction) {
        return vListView != null && vListView.canScrollVertically(direction);
    }

    @Override
    public void onFlingOver(int y, long duration) {
        if (vListView != null) {
            vListView.smoothScrollBy(y, (int) duration);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MyProfitsContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
